﻿using KZ.Express.ENT;
using KZ.Express.Resource;
using KZ.Express.Task;
using KZ.Utilities;
using System;
using System.Data;

namespace KZ.Express.Presenter
{
    public interface IOrderDetailView : IViewBase
    {
        int OrderNumberLength { get; }
    }

    public class OrderDetailPresenter : PresenterBase
    {
        new IOrderDetailView _view;
        new OrderTask _task;

        public OrderDetailPresenter(IOrderDetailView view)
            : base(view, new OrderENT())
        {
            _view = view;
            _task = new OrderTask();
        }

        public DataTable LoadOriginalPriceList(int carrierID, int supplierID, DateTime date)
        {
            try
            {
                base._view.SetMessage(String.Empty, true);
                return _task.LoadOriginalPriceList(carrierID, supplierID, date);
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }

        public DataTable LoadCustomerPriceList(int carrierID, int supplierID, int customerID,DateTime date)
        {
            try
            {
                base._view.SetMessage(String.Empty, true);
                return _task.LoadCustomerPriceList(carrierID, supplierID, customerID, date);
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }

        public decimal LoadPrice(int priceListID, int carrierID, int countryID, string type, decimal weight)
        {
            try
            {
                base._view.SetMessage(String.Empty, true);
                return _task.LoadPrice(priceListID, carrierID, countryID, type, weight);
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }

        public override void Save()
        {
            try
            {
                int ordernumberLenght = _view.OrderNumberLength;
                OrderENT order = _view.Entity as OrderENT ?? new OrderENT();

                if (ordernumberLenght > 0 && ordernumberLenght != order.OrderNumber.Length)
                    throw new BusinessLogicException("Độ dài số bill không đúng");

                if (_view.Action == ActionName.Insert)
                {
                    if (_task.Insert(order) > 0)
                    {
                        _view.Entity = null;
                        _view.SetMessage(Message.BLM_SUC_INSERT.ToString(), true);
                    }
                }
                else if (_view.Action == ActionName.Update)
                {
                    if (_task.Update(order) > 0)
                    {
                        _view.SetMessage(Message.BLM_SUC_UPDATE.ToString(), true);
                    }
                }
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }
    }
}